﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls;

namespace Organizer.pages
{
    public partial class Calendar : System.Web.UI.Page
    {
        //TODO:Rever tratamento de excepções     

        #region Properties
        private string CalendarId { get { return hfCalendarId.Value; } set { hfCalendarId.Value = value; } }
        private int UserId { get { return Int32.Parse(Page.RouteData.Values["UserId"].ToString()); } }
        private Dictionary<string, string> List;
        private BLOrganizer.BLCalendar BL;
        private StringBuilder sb;
        #endregion

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                mdlDay.Hide();
                GetList();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void Calendario_DayRender(object sender, DayRenderEventArgs e)
        {
            try
            {
                if (sb == null) { sb = new StringBuilder(); }
                int eDay = e.Day.Date.Day;
                int eMonth = e.Day.Date.Month;
                int eYear = e.Day.Date.Year;
                sb.Append(eDay);
                sb.Append("-");
                sb.Append(eMonth);
                sb.Append("-");
                sb.Append(eYear);
                if (List.Count >0 && List.ContainsKey(sb.ToString()) == true)
                {
                    e.Cell.BackColor = ColorTranslator.FromHtml(List[sb.ToString()]);
                }              
                sb.Clear();                      
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void Calendario_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                Clear();
                mdlDay.Show();
                if (BL == null) { BL = new BLOrganizer.BLCalendar(); }
                List<string> Day = BL.GetCalendarDay(UserId, Calendario.SelectedDate.Day, Calendario.SelectedDate.Month, Calendario.SelectedDate.Year);
                if (Day.Count.Equals(0)) { return; }
                txtColor.Text = Day[0].ToString().Remove(0, 1);
                txtMsg.Text = Day[1].ToString();
                CalendarId = Day[2].ToString();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void Calendario_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
        {
            try
            {
                mdlDay.Hide();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                Clear();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (BL == null) { BL = new BLOrganizer.BLCalendar(); }
                if (CalendarId.Equals("0")) { return; }
                BL.DeleteCalendar(Int32.Parse(CalendarId));
                CalendarId = "0";
                Clear();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (BL == null) { BL = new BLOrganizer.BLCalendar(); }
                DateTime date = Convert.ToDateTime(Calendario.SelectedDate.ToShortDateString());
                BL.AddCalendar(UserId, date, "#" + txtColor.Text, txtMsg.Text);
                Clear();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        #endregion

        #region Methods
        private void GetList()
        {
            try
            {
                if (BL == null) { BL = new BLOrganizer.BLCalendar(); }
                List = BL.GetCalendar(UserId, DateTime.Today.Month, DateTime.Today.Year);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void Clear()
        {
            try
            {
                txtColor.Text = string.Empty;
                txtMsg.Text = string.Empty;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void FeedBackMsg(string Text, int AlertType)
        {
            try
            {
                Main Mn = (Main)Page.Master;
                Mn.FeedBackMsg(Text, AlertType);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}